﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockMatch.WebSite
{
    public struct DayWinResult
    {
        public decimal Warning { get; set; }
        public decimal Open { get; set; }
        public decimal Principal { get; set; }
        public decimal Fee { get; set; }
        public double Interest { get; set; }
    }

    public struct WeekWinResult
    {
        public decimal Warning { get; set; }
        public decimal Open { get; set; }
        public decimal Principal { get; set; }
        public decimal Fee { get; set; }
        public double Interest { get; set; }
    }

    public struct StockResult
    {
        public bool Discount { get; set; }
        public decimal Interest { get; set; }

        public decimal OpenLine { get; set; }
        public decimal WarningLine { get; set; }
    }

    public class MathProject
    {
        public static DayWinResult MathRiskMargin(decimal money, int lever)
        {
            if (lever == 5 || lever == 10 || lever == 15)
            {
                if (lever == 5)
                {
                    return new DayWinResult
                    {
                        Warning = money*110/100,
                        Open = money*107/100,
                        Principal = Math.Floor(money/lever),
                        Fee = Math.Round(money*1.2M/1000, 4),
                        Interest = 1.2
                    };
                }
                else if (lever == 10)
                {
                    return new DayWinResult
                    {
                        Warning = money*106/100,
                        Open = money*104/100,
                        Principal = Math.Floor(money/lever),
                        Fee = Math.Round(money*1.5M/1000, 4),
                        Interest = 1.5
                    };
                }
                else
                {
                    return new DayWinResult
                    {
                        Warning = money*105/100,
                        Open = money*103/100,
                        Principal = Math.Floor(money/lever),
                        Fee = Math.Round(money*2/1000, 4),
                        Interest = 2
                    };
                }

            }

            return new DayWinResult {Warning = 0, Open = 0, Principal = 0, Fee = 0, Interest = 0};
        }

        public static string GetLismit(decimal money, int lever)
        {
            StringBuilder sb = new StringBuilder();

            string noneLimit ="none";
            string limit = "inline";
            string limitinfo = string.Empty;
            if (lever == 5)
            {
                if (money <= 500000)
                {
                    noneLimit = "inline";
                    limit = "none";
                    limitinfo= "";
                }
                else if (money <= 1000000)
                {
                    limitinfo= "单股不超总操盘资金的50%";
                }
                else
                {
                    limitinfo = "单股不超总操盘资金的50%（创业板33%）";
                }
            }
            else if (lever == 10)
            {
                if (money <= 200000)
                {
                    noneLimit = "inline";
                    limit = "none";
                    limitinfo = "";
                }
                else if (money <= 1000000)
                {
                    limitinfo = "单股不超总操盘资金的50%";
                }
            }
            else if (lever == 15)
            {
                if (money <= 100000)
                {
                    noneLimit = "inline";
                    limit = "none";
                    limitinfo = "";
                }
                else
                {
                    limitinfo = "单股不超总操盘资金的50%";
                }
            }

            sb.Append("投资沪深A股，");
            sb.AppendFormat("<q id='noneLimit' style='display: {0};'>仓位不限制</q>", noneLimit);
            sb.AppendFormat("<q style='color: rgb(205, 24, 24); display: {0};' id='limit'>仓位有限制</q>", limit);
            sb.Append("，盈利全归您");
            sb.Append("<br>");
            sb.AppendFormat("<q style='color:#cd1818' id='limitinfo'>{0}</q>", limitinfo);
            return sb.ToString();
        }

        public static string PositionLimits(decimal money, int lever)
        {
            string limitinfo = string.Empty;
            if (lever == 5)
            {
                if (money <= 500000)
                {
                    limitinfo = "不限制";
                }
                else if (money <= 1000000)
                {
                    limitinfo = "限制单股不超总操盘资金的50%";
                }
                else
                {
                    limitinfo = "限制单股不超总操盘资金的50%（创业板33%）";
                }
            }
            else if (lever == 10)
            {
                if (money <= 200000)
                {
                    limitinfo = "不限制";
                }
                else if (money <= 1000000)
                {
                    limitinfo = "限制单股不超总操盘资金的50%";
                }
            }
            else if (lever == 15)
            {
                if (money <= 100000)
                {
                    limitinfo = "不限制";
                }
                else
                {
                    limitinfo = "限制单股不超总操盘资金的50%";
                }
            }
            return limitinfo;
        }

        public static WeekWinResult MathRiskMargin(decimal money)
        {
            return new WeekWinResult
            {
                Warning = money*110/100,
                Open = money*107/100,
                Principal = Math.Floor(money/5),
                Fee = Math.Round(money*1.2M/1000, 4),
                Interest = 1.2
            };
        }

        public static string GetLismit(decimal money)
        {
            StringBuilder sb = new StringBuilder();

            string noneLimit = "none";
            string limit = "inline";
            string limitinfo = string.Empty;

            if (money <= 500000)
            {
                noneLimit = "inline";
                limit = "none";
                limitinfo= "";
            }
            else if (money <= 1000000)
            {
                limitinfo= "单股不超总操盘资金的50%";
            }
            else
            {
                limitinfo= "单股不超总操盘资金的50%（创业板33%）";
            }
            sb.Append("投资沪深A股，");
            sb.AppendFormat("<q id='noneLimit' style='display: {0};'>仓位不限制</q>",noneLimit);
            sb.AppendFormat("<q style='color: rgb(205, 24, 24); display: {0};' id='limit'>仓位有限制</q>", limit);
            sb.Append("，盈利七成归您");
            sb.Append("<br>");
            sb.AppendFormat("<q style='color:#cd1818' id='limitinfo'>{0}</q>",limitinfo);
            return sb.ToString();
        }

        public static string PositionLimits(decimal money)
        {
            string limitinfo = string.Empty;
            if (money <= 500000)
            {

                limitinfo = "不限制";
            }
            else if (money <= 1000000)
            {
                limitinfo = "单股不超总操盘资金的50%";
            }
            else
            {
                limitinfo = "单股不超总操盘资金的50%（创业板33%）";
            }
            return limitinfo;
        }

        public static StockResult GetScheme(decimal money, int cycle, decimal lever, int fixRate)
        {
            decimal interest = 190;
            bool discount = false;

            if (money >= 1000000)
            {
                if (cycle >= 3)
                {
                    interest = 160;
                }
                else if (cycle >= 1)
                {
                    interest = 170;
                }
            }
            else if (money >= 100000)
            {
                if (cycle >= 3)
                {
                    interest = 170;
                }
                else if (cycle >= 1)
                {
                    interest = 180;
                }
            }
            else if (money >= 1000)
            {
                if (cycle >= 3)
                {
                    interest = 180;
                }
                else if (cycle >= 1)
                {
                    interest = 190;
                }
            }
            if (lever <= 2)
            {
                discount = true;
                interest = interest - (5 - lever + 2)*10;
                if (fixRate > 1 && interest >= fixRate)
                {
                    discount = false;
                    interest = fixRate;
                }
            }
            else if (lever < 5)
            {
                discount = true;
                interest = interest - (5 - lever + 1)*10;
                if (fixRate > 1 && interest >= fixRate)
                {
                    discount = false;
                    interest = fixRate;
                }
            }
            else if (lever == 5)
            {
                if (fixRate > 1 && interest >= fixRate)
                {
                    interest = fixRate;
                }
            }

            return new StockResult
            {
                Discount=discount,
                Interest=interest,
                OpenLine=Math.Round(money*1.07M),
                WarningLine = Math.Round(money * 1.1M)
            };

        }
        public static decimal GetIntention(decimal money)
        {
            return money/100;
        }
    }
}
